// 由于iframe天然隔离性，所以没有激活和失活的步骤，只需要注意应用间的通信即可
class SandboxWindow {
  constructor(context, frameWindow) {
    return new Proxy(frameWindow, {
      get(target, name) {
        if (name in context) {
          return context[name];
        } else if (typeof target[name] === 'function' && /^[a-z]/.test(name)) {
          return target[name].bind && target[name].bind(target);
        } else {
          return target[name];
        }
      },
      set(target, name, value) {
        if (name in context) {
          return (context[name] = value);
        }
        target[name] = value;
      },
    });
  }
}
// 需要全局共享的变量
const context = {
  document: window.document,
  history: window.history,
  location: window.location,
};
// 创建 iframe
const userInputUrl = '';
const iframe = document.createElement('iframe', { url: userInputUrl });
document.body.appendChild(iframe);
const sandboxGlobal = iframe.contentWindow;
// 创建沙箱
const newSandboxWindow = new SandboxWindow(context, sandboxGlobal);
newSandboxWindow.city = 'beijing';
console.log(newSandboxWindow.city);
// beijing